package com.easondongh.common.response;

import com.easondongh.common.enums.ResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

/**
 * @author EasonDongH
 * @date 2020/4/28 16:23
 */
@Data
@AllArgsConstructor
public class ResponseObj<T> {

    private Integer code = 200;
    private String msg;
    private T data;

    public ResponseObj(ResponseEnum responseEnum) {
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMsg();
    }

    public ResponseObj(ResponseEnum responseEnum, T data) {
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMsg();
        this.data = data;
    }

    public ResponseObj(BodyBuilder body) {
        this.code = body.getCode();
        this.msg = body.getMsg();
        this.data = body.getData();
    }

    public ResponseObj(T data) {
        this(ResponseEnum.SUCCESS, data);
    }

    public static ResponseObj QuerySuccess = new ResponseObj(ResponseEnum.QUERY_SUCCESS);
    public static ResponseObj QueryFail = new ResponseObj(ResponseEnum.QUERY_FAIL);
    public static ResponseObj AddSuccess = new ResponseObj(ResponseEnum.ADD_SUCCESS);
    public static ResponseObj AddFail = new ResponseObj(ResponseEnum.ADD_FAIL);
    public static ResponseObj DeleteSuccess = new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    public static ResponseObj DeleteFail = new ResponseObj(ResponseEnum.DELETE_FAIL);
    public static ResponseObj UpdateSuccess = new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    public static ResponseObj UpdateFail = new ResponseObj(ResponseEnum.UPDATE_FAIL);

    @Getter
    private class BodyBuilder {
        private Integer code = 200;
        private String msg;
        private T data;

        public BodyBuilder status(ResponseEnum responseEnum) {
            this.code = responseEnum.getCode();
            this.msg = responseEnum.getMsg();
            return this;
        }

        public  BodyBuilder data(T data) {
            this.data = data;
            return this;
        }

    }
}
